{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import re\n",
      "from functools import wraps\n",
      "\n",
      "with open('O00001.nc', encoding='cp1251') as f:\n",
      "    src = f.read()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# comments\n",
      "src = re.sub(r\"N\\d+ \", r\"\", src)\n",
      "src = re.sub(r\"\\'[\\S ]+\", \"\", src)\n",
      "src = re.sub(r\"\\([\\S ]+\\)\", \"\", src)\n",
      "src = re.sub(r\"\\n+\", r\"\\n\", src)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# program start\n",
      "m = re.search(r'''\n",
      "    ^START\\n\n",
      "    \\'\\((O\\d+)\\)\\n\n",
      "    %\\n\n",
      "    ''', src, re.M | re.VERBOSE)\n",
      "out += ['%', m.group(1)]\n",
      "src = src[m.end():]\n",
      "print(src)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(PROGRAM PRODUCED  - 04 JAN 14)\n",
        "(NC PROGRAMMER - J BLOGGS)\n",
        ":1234\n",
        "N10 G21 G80 G40\n",
        "N20 G50 S1200 M42\n",
        "'(OP 1   \u0427\u0418\u0421\u0422\u041e\u0412\u0410\u042f   TOOL 1  EBLC 08 16 95 5 80 CT525 P)\n",
        "'(TOOL TIP RAD 0.8)\n",
        "N30 G0 T11            'Select TOOL 1 and OFFSET Number 1\n",
        "N40 G50 (X... Z...)         'Enter tool reference values at machine\n",
        "N50 G50 S1200\n",
        "N60 G97 S111 M03 G99\n",
        "N70 G0 X37. Z-21.8 M09\n",
        "N80 G1 Z-10. F0.064\n",
        "N90 X21.\n",
        "N100 Z0.\n",
        "N110 X18.4\n",
        "N120 G0 X40. Z5. M09\n",
        "N130 T100\n",
        "'(OP 2   \u0427\u0418\u0421\u0422\u041e\u0412\u041e\u0419 \u041f\u0420\u041e\u0425\u041e\u0414   TOOL 2   FLAT - 10MM)\n",
        "'(EFFECTIVE DIAMETER 10)\n",
        "N140 T22      ' Select MILLING TOOL type 1\n",
        "N150 G112\n",
        "N160 G1 X46.8 Z2. C0. F4000\n",
        "N170 X46.8 Z2. C0. F4000\n",
        "N180 X46.8 Z1. C0. F4000\n",
        "N190 X23.4 Y0. Z-11. F240\n",
        "N200 G2 X20.4 Y-5.196 R6. F480\n",
        "N210 G1 X-2.7 Y-18.533\n",
        "N220 G2 X-14.7 Y-11.605 R8.\n",
        "N230 G1 Y13.337\n",
        "N240 G2 X-4.2 Y19.399 R7.\n",
        "N250 G1 X20.4 Y5.196\n",
        "N260 G2 X23.4 Y0. R6.\n",
        "N270 G1 X46.8 Z0. C0. F4000\n",
        "N280 X23.4 Z-12. F240\n",
        "N290 G2 X20.4 Y-5.196 R6. F480\n",
        "N300 G1 X-2.7 Y-18.533\n",
        "N310 G2 X-14.7 Y-11.605 R8.\n",
        "N320 G1 Y13.337\n",
        "N330 G2 X-4.2 Y19.399 R7.\n",
        "N340 G1 X20.4 Y5.196\n",
        "N350 G2 X23.4 Y0. R6.\n",
        "N360 G1 X46.8 Z-1. C0. F4000\n",
        "N370 X23.4 Z-13. F240\n",
        "N380 G2 X20.4 Y-5.196 R6. F480\n",
        "N390 G1 X-2.7 Y-18.533\n",
        "N400 G2 X-14.7 Y-11.605 R8.\n",
        "N410 G1 Y13.337\n",
        "N420 G2 X-4.2 Y19.399 R7.\n",
        "N430 G1 X20.4 Y5.196\n",
        "N440 G2 X23.4 Y0. R6.\n",
        "N450 G1 X46.8 Z2. C0. F4000\n",
        "N460 T200\n",
        "'(OP 3   \u0421\u0412\u0415\u0420\u041b\u0415\u041d\u0418\u0415 \u041e\u0422\u0412\u0415\u0420\u0421\u0422\u0418\u0419   TOOL 3   DRILL-2.5 DIA)\n",
        "'(TOOL DIAMETER 2.5, HOLE DIAMETERS 2.5)\n",
        "N470 T320      ' Select MILLING TOOL type 4\n",
        "N480 G1 X27. Z2. C0. F4000\n",
        "N490 G98 G83 X27. C0. Z-13. R1. F120\n",
        "N500 X27. C120.\n",
        "N510 X27. C240.\n",
        "N520 G80\n",
        "N530 G1 X27. Z2. C360. F4000\n",
        "N540 G98 G83 X27. C360. Z-16. R1. F120\n",
        "N550 X27. C480.\n",
        "N560 X27. C600.\n",
        "N570 G80\n",
        "N580 G1 X27. Z2. C720. F4000\n",
        "N590 G98 G83 X27. C720. Z-19. R1. F120\n",
        "N600 X27. C840.\n",
        "N610 X27. C960.\n",
        "N620 G80\n",
        "N630 T300\n",
        "'(OP 4   \u041d\u0410\u0420\u0415\u0417\u0410\u041d\u0418\u0415 \u0420\u0415\u0417\u042c\u0411\u042b \u041c\u0415\u0422\u0427\u0418\u041a\u041e\u041c   TOOL 5   TAP -3MM X0.5 PITCH)\n",
        "'(TOOL DIAMETER 3, HOLE DIAMETERS 2.5)\n",
        "N640 T55      ' Select MILLING TOOL type 5\n",
        "N650 G1 X27. Z2. C0. F4000\n",
        "N660 G98 G8*** X27. C0. Z-19. R1. F100\n",
        "N670 X27. C120.\n",
        "N680 X27. C240.\n",
        "N690 G80\n",
        "N700 T500\n",
        "N710 M30\n",
        "%\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 50
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(src)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Operation List    POST: Alpha Standard C-Y Lathe\n",
        "--------------------------------------------------\n",
        "OP 1   \u0427\u0438\u0441\u0442\u043e\u0432\u0430\u044f   TOOL 1  EBLC 08 16 95 5 80 CT525 P\n",
        "       TOOL TIP RAD 0.8\n",
        "       Feed Distance: 31.1   Time for OP 1: 4m 24s\n",
        "OP 2   \u0427\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434   TOOL 2   FLAT - 10MM\n",
        "       EFFECTIVE DIAMETER 10\n",
        "       Feed Distance: 414   Time for OP 2: 0m 57s\n",
        "OP 3   \u0421\u0432\u0435\u0440\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0440\u0441\u0442\u0438\u0439   TOOL 3   DRILL-2.5 DIA\n",
        "       TOOL DIAMETER 2.5, HOLE DIAMETERS 2.5\n",
        "       \u0421\u0432\u0435\u0440\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0440\u0441\u0442\u0438\u0439   TOOL 3   DRILL-2.5 DIA\n",
        "       TOOL DIAMETER 2.5, HOLE DIAMETERS 2.5\n",
        "       \u0421\u0432\u0435\u0440\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0440\u0441\u0442\u0438\u0439   TOOL 3   DRILL-2.5 DIA\n",
        "       TOOL DIAMETER 2.5, HOLE DIAMETERS 2.5\n",
        "       Feed Distance: 153   Time for OP 3: 1m 24s\n",
        "OP 4   \u041d\u0430\u0440\u0435\u0437\u0430\u043d\u0438\u0435 \u0440\u0435\u0437\u044c\u0431\u044b \u043c\u0435\u0442\u0447\u0438\u043a\u043e\u043c   TOOL 5   TAP -3MM X0.5 PITCH\n",
        "       TOOL DIAMETER 3, HOLE DIAMETERS 2.5\n",
        "       Feed Distance: 120   Time for OP 4: 0m 03s\n",
        "--------------------------------------------------\n",
        "Total Feed Distance ........................ 718.1\n",
        "Tool Change Time .......................... 0m 12s\n",
        "Total Time ................................ 7m 00s\n",
        "--------------------------------------------------\n",
        "Material: Low Carbon Steel\n",
        "Prefered Chip Former PP\n",
        "--------------------------------------------------\n",
        "START\n",
        "%\n",
        ":1234\n",
        "G21 G80 G40\n",
        "G50 S1200 M42\n",
        "G0 T11            \n",
        "G50          \n",
        "G50 S1200\n",
        "G97 S111 M03 G99\n",
        "G0 X37. Z-21.8 M09\n",
        "G1 Z-10. F0.064\n",
        "X21.\n",
        "Z0.\n",
        "X18.4\n",
        "G0 X40. Z5. M09\n",
        "T100\n",
        "T22      \n",
        "G112\n",
        "G1 X46.8 Z2. C0. F4000\n",
        "X46.8 Z2. C0. F4000\n",
        "X46.8 Z1. C0. F4000\n",
        "X23.4 Y0. Z-11. F240\n",
        "G2 X20.4 Y-5.196 R6. F480\n",
        "G1 X-2.7 Y-18.533\n",
        "G2 X-14.7 Y-11.605 R8.\n",
        "G1 Y13.337\n",
        "G2 X-4.2 Y19.399 R7.\n",
        "G1 X20.4 Y5.196\n",
        "G2 X23.4 Y0. R6.\n",
        "G1 X46.8 Z0. C0. F4000\n",
        "X23.4 Z-12. F240\n",
        "G2 X20.4 Y-5.196 R6. F480\n",
        "G1 X-2.7 Y-18.533\n",
        "G2 X-14.7 Y-11.605 R8.\n",
        "G1 Y13.337\n",
        "G2 X-4.2 Y19.399 R7.\n",
        "G1 X20.4 Y5.196\n",
        "G2 X23.4 Y0. R6.\n",
        "G1 X46.8 Z-1. C0. F4000\n",
        "X23.4 Z-13. F240\n",
        "G2 X20.4 Y-5.196 R6. F480\n",
        "G1 X-2.7 Y-18.533\n",
        "G2 X-14.7 Y-11.605 R8.\n",
        "G1 Y13.337\n",
        "G2 X-4.2 Y19.399 R7.\n",
        "G1 X20.4 Y5.196\n",
        "G2 X23.4 Y0. R6.\n",
        "G1 X46.8 Z2. C0. F4000\n",
        "T200\n",
        "T320      \n",
        "G1 X27. Z2. C0. F4000\n",
        "G98 G83 X27. C0. Z-13. R1. F120\n",
        "X27. C120.\n",
        "X27. C240.\n",
        "G80\n",
        "G1 X27. Z2. C360. F4000\n",
        "G98 G83 X27. C360. Z-16. R1. F120\n",
        "X27. C480.\n",
        "X27. C600.\n",
        "G80\n",
        "G1 X27. Z2. C720. F4000\n",
        "G98 G83 X27. C720. Z-19. R1. F120\n",
        "X27. C840.\n",
        "X27. C960.\n",
        "G80\n",
        "T300\n",
        "T55      \n",
        "G1 X27. Z2. C0. F4000\n",
        "G98 G8*** X27. C0. Z-19. R1. F100\n",
        "X27. C120.\n",
        "X27. C240.\n",
        "G80\n",
        "T500\n",
        "M30\n",
        "%\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}